<template>
  <div class="app-container">
    <div style="width: 100%; margin-bottom: 10px">
      <el-button type="primary" size="mini" @click="addCommand" :disabled="approveStatus === '通过后不可再次提交审批'" >{{addButtonStatus}}</el-button>
<!--      <el-button-->
<!--        type="warning"-->
<!--        plain-->
<!--        icon="el-icon-download"-->
<!--        size="mini"-->
<!--      >导出</el-button>-->
    </div>
    <el-table border :data="detailList" style="width: 100%">
      <el-table-column label="编号" width="50" type="index" align="center" fixed="left"
                       :index="index=>index+1"/>
      <el-table-column prop="resourceName" label="食材名称" align="center" min-width="180">
        <template #default="scope">
          <span v-show="!scope.row.editFlag">{{scope.row.resourceName}}</span>
          <el-select v-show="scope.row.editFlag" v-model="scope.row.resourceId" placeholder="请选择食材" filterable clearable :style="{width: '100%'}" @change="id=>resourceIdChange(scope.row,id)">
            <el-option v-for="(item, index) in resourceOptions" :key="index" :label="item.name"
                       :value="item.id" :disabled="item.disabled"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="amount" label="计划数量" align="center" width="180">
        <template #default="scope">
          <span v-show="!scope.row.editFlag">{{scope.row.amount}}</span>
          <el-input v-show="scope.row.editFlag"
                    v-model="scope.row.amount">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="unit" label="计划单位" align="center" width="180">
        <template #default="scope">
          <span >{{scope.row.unit}}</span>
<!--          <el-input v-show="scope.row.editFlag"-->
<!--                    v-model="scope.row.unit">-->
<!--          </el-input>-->
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" width="160" align="center"/>
      <el-table-column label="更新人" prop="updateBy" width="160" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right" width="160">
        <template #default="scope">
          <div style="">
            <el-button size="small"
                       type="text"
                       icon="CircleCheck"
                       v-show="scope.row.editFlag"
                       :disabled="approveStatus === '通过后不可再次提交审批'"
                       @click="submit(scope.row, $index)">保存
            </el-button>
            <el-button size="small"
                       type="text"
                       icon="CircleClose"
                       v-show="scope.row.editFlag"
                       :disabled="approveStatus === '通过后不可再次提交审批'"
                       @click="cancel(scope.row, $index)">取消
            </el-button>
            <el-button size="small"
                       icon="Edit"
                       type="text"
                       v-show="!scope.row.editFlag"
                       :disabled="approveStatus === '通过后不可再次提交审批'"
                       @click="edit(scope.row, $indx)"> 编辑
            </el-button>
            <el-button size="small"
                       icon="Delete"
                       type="text"
                       v-show="!scope.row.editFlag"
                       :disabled="approveStatus === '通过后不可再次提交审批'"
                       @click="del(scope.row, $index)">删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>


<script>
import { list, listAll, add, update, del } from "@/api/store/plan/detail";
import { get } from "@/api/store/plan/temp";
import { getAllResource } from "@/api/store/resource";
import { getByActivityId, addApproveMission } from "@/api/approve/mission";

export default {
  name: "BaseDetail",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      id: undefined,
      // 遮罩层
      loading: true,
      // 菜品明细表格数据
      detailList: [],
      resourceOptions: [],
      approveStatus: "提交审批",
      addButtonStatus: "添加",
      statusOptions: [{
        label: "是",
        value: "是"
      },{
        label: "否",
        value: "否"
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      tempData:'',
    };
  },
  created() {
    this.id = this.$route.query && this.$route.query.id;
    get(this.id).then(response => {
      this.tempData = response.data;
    });
    this.getList();
    getAllResource().then(res => {
      this.resourceOptions=res.rows
    })
    getByActivityId({
      activityId: this.id,
      activityType: "采购计划详情审批"
    }).then(res => {
      if (res.data == null) {
        return;
      }
      this.approveId = res.data.id;
      if (res.data.status === "拒绝") {
        this.approveStatus = "再次提交审批";
      } else if (res.data.status === "通过") {
        this.approveStatus = "通过后不可再次提交审批";
        this.addButtonStatus = "通过后不可添加或更改";
      } else {
        this.approveStatus = "查看审批";
      }
    })
  },
  methods: {
    //选择食材
    resourceIdChange(row,id){
      row.unit=this.resourceOptions.find(item=>item.id===id).unit
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listAll({
        planId: this.id,
      }).then(response => {
          this.detailList = response.rows;
          this.loading = false;
        }
      );
    },
    addCommand: function() {
      var detail = {
        planId: this.id
      }
      add(detail).then(res => {
        this.getList();
      })
    },
    submitApprove: function() {
      if (this.approveStatus === "查看审批") {
        this.$router.push("/approve/detail/" + this.approveId);
        return;
      }
      addApproveMission({
        activityId: this.id,
        activityType: "采购计划详情审批",
        name: "采购计划审批",
        status: "审批中",
      }).then(res => {
        this.$modal.msgSuccess("提交成功");
        this.approveStatus = "查看审批";
        this.approveId = res.data.id;
      });
    },
    submit: function(row, index) {
      let updateData = {
        id: row.id,
        resourceId: row.resourceId,
        amount: row.amount,
        unit: row.unit,
        status: row.status,
      }
      update(updateData).then(res => {
        this.$modal.msgSuccess("更新成功");
        row.editFlag = false;
        this.getList();
      })
    },
    cancel: function(row, index) {
      row.editFlag = false;
      this.getList();
    },
    edit: function(row, index) {
      row.editFlag = true;
      this.$set(this.detailList, index, row);
    },
    del: function(row, index) {
      del(row.id).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      });
    }
  }
};
</script>
